<?php

class TipesController extends \BaseController {

	/**
	 * Display a listing of tipes
	 *
	 * @return Response
	 */
	public function index()
	{		
		$tipes = Tipe::where('is_active', '=', '1')->orderBy('nama','ASC')->get();
		return View::make('tipes.index', compact('tipes'));
	}

	/**
	 * Show the form for creating a new tipe
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('tipes.create');
	}

	/**
	 * Store a newly created tipe in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Tipe::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Tipe::create($data);

		return Redirect::route('tipes.index');
	}

	/**
	 * Display the specified tipe.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$tipe = Tipe::findOrFail($id);

		return View::make('tipes.show', compact('tipe'));
	}

	/**
	 * Show the form for editing the specified tipe.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$tipe = Tipe::find($id);

		return View::make('tipes.edit', compact('tipe'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$validator = Validator::make($data = Input::all(), Tipe::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Tipe::create($data);

		//---------------------------------------------------------------

		$tipe = Tipe::findOrFail($id);

		// $validator = Validator::make($data = Input::all(), Tipe::$rules);

		// if ($validator->fails())
		// {
		// 	return Redirect::back()->withErrors($validator)->withInput();
		// }

		// $tipe->update($data);

		$tipe->is_active = 0;

		$tipe->save();

		return Redirect::route('tipes.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Tipe::destroy($id);

		$tipe = Tipe::findOrFail($id);

		$tipe->is_active = 0;
		$tipe->user_id = Auth::user()->id;
		$tipe->action = 3;

		$tipe->save();

		return Redirect::route('tipes.index');
	}

}